package project.base.common.config

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestControllerAdvice
import project.base.common.util.ResponseResult
import project.base.common.util.ResponseUtil

/**
 * 全局异常处理
 * @author 三味
 * @since 2022/3/3 16:36
 */
@RestControllerAdvice
class RestExceptionHandler {

    private val logger: Logger = LoggerFactory.getLogger(this.javaClass)

    @ExceptionHandler(Exception::class)
    @ResponseBody
    fun handler(exception: Exception): ResponseResult<Any?> {
        logger.error("全局异常:{}", exception)
        return ResponseUtil.failed(HttpStatus.EXPECTATION_FAILED,exception.message)
    }
}
